package com.adcc.ags.gms.gw.controller;

import com.adcc.ags.gms.core.util.Constant;
import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.core.vo.ResultState;
import com.adcc.ags.gms.core.vo.ResultVO;
import com.adcc.ags.gms.gw.service.QueueMonitorService;
import com.adcc.ags.gms.gw.vo.QueueInfoVO;
import com.adcc.ags.gms.mq.service.QueueService;
import com.google.common.base.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value="/queueMonitor",produces = MediaType.APPLICATION_JSON_VALUE)
public class QueueMonitorController {

    @Autowired
    private QueueService queueService;

    @Autowired
    private QueueMonitorService queueMonitorService;

    @Autowired
    private LogFactory log;

    /**
     * 取得队列信息
     * @return
     */
    @RequestMapping(value = "/getQueueInfo", method = RequestMethod.GET)
    public @ResponseBody ResultVO getQueueInfo() {
        ResultVO resultVO = new ResultVO();
        try {
            List<QueueInfoVO> list = queueMonitorService.getQueueInfo();
            resultVO.getData().put("queueInfoVO", list);
        } catch (Exception e) {
            log.error(QueueMonitorController.class, "getQueueInfo() error", e);
            resultVO.setCode(-1);
            resultVO.setState(ResultState.FAILURE);
            resultVO.setReason(Constant.GMS_SERVER_ERROR);
        }
        return resultVO;
    }

    @RequestMapping(value = "/clearQueue", method = RequestMethod.GET)
    public @ResponseBody ResultVO clearQueue(String queueName){
        ResultVO resultVO = new ResultVO();
        try {
            Optional<Boolean> optional = queueService.clearQueue(queueName);
            if(!optional.isPresent()){
                resultVO.setCode(-1);
                resultVO.setState(ResultState.FAILURE);
                resultVO.setReason(Constant.GMS_PCF_ERROR);
            }
        } catch (Exception e) {
            log.error(QueueMonitorController.class, "getQueueInfo() error", e);
            resultVO.setCode(-1);
            resultVO.setState(ResultState.FAILURE);
            resultVO.setReason(Constant.GMS_SERVER_ERROR);
        }
        return resultVO;
    }


}
